import requests
import re
import xlwt
from multiprocessing import Pool
def get_one_page(url):
    headers = {
        'authority': 's.taobao.com',
        'method': 'GET',
        'path': '/api?_ksTS=1507342673846_219&callback=jsonp220&ajax=true&m=customized&sourceId=tb.index&_input_charset=utf-8&bcoffset=0&commend=all&suggest=history_3&source=suggest&search_type=item&ssid=s5-e&suggest_query=&spm=a21bo.50862.201856-taobao-item.2&q=%E7%94%B7%E8%A3%A4&s=36&initiative_id=tbindexz_20171007&imgfile=&wq=&ie=utf8&rn=cfb3eea8ae196efafa5f813ae2df21f0',
        'scheme': 'https',
        'accept': 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
        'accept-encoding': 'gzip, deflate, sdch, br',
        'accept-language': 'zh-CN,zh;q=0.8',
        'cookie': 'miid=1504824888974818073; UM_distinctid=15e237dee84f7-010596f6e-4349072c-100200-15e237dee861; __guid=154677242.4031585706812497000.1503836519346.7197; hng=CN%7Czh-CN%7CCNY%7C156; thw=cn; uc3=sg2=UoTfKLITZlXL0Is5hM5B8XwcEuTdKWFDpygC46JxjmY%3D&nk2=CcYx2hamYD8%3D&id2=UUtO8RnzigwHKQ%3D%3D&vt3=F8dBzWk433CNuDrC6fk%3D&lg2=V32FPkk%2Fw0dUvg%3D%3D; uss=AQGnxugLA99LFC3dRcR7YtJLXj1y8oHxZ6CNxGyZOT6uM3NzUAGT0EoogA%3D%3D; lgc=justijin; tracknick=justijin; mt=np=&ci=5_1; _cc_=W5iHLLyFfA%3D%3D; tg=0; t=2010491725ba88cebe30bf46b3adf8c7; cookie2=610d7a48bb9c6016eb4fd9788b3b5f74; v=0; _tb_token_=e97f14ee5be5e; alitrackid=www.taobao.com; lastalitrackid=www.taobao.com; JSESSIONID=1A74F2E4738351A204F0CACF5D2DC712; isg=AhkZNMCmpFBeBXhSjyEkH3TyKAVftEV_wW-KEzvOVsC8QjnUg_YdKIcSMAnX; monitor_count=18; cna=uwjEEXc6M3kCAd9KMy5L8J5W',
        'referer': 'https://s.taobao.com/search?initiative_id=tbindexz_20171007&ie=utf8&spm=a21bo.50862.201856-taobao-item.2&sourceId=tb.index&search_type=item&ssid=s5-e&commend=all&imgfile=&q=%E7%94%B7%E8%A3%A4&suggest=history_3&_input_charset=utf-8&wq=&suggest_query=&source=suggest',
        'user-agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
        'x-requested-with': 'XMLHttpRequest'
    }
    try:
        response=requests.get(url, headers=headers)
        if response.status_code==200:
            return  response.text
        return None
    except RecursionError:
        return None
def get_information(urls):
    ws = xlwt.Workbook()
    w = ws.add_sheet("男裤信息", cell_overwrite_ok=True)
    sa = ["序号", "商品价格", "商品产地", "有多少人付款", "商品店名", "商品名"]
    for c in range(0, len(sa)):
        w.write(0, c, sa[c])
    d1 = d2 = d3 = d4 = d5 = 1
    for url in urls:
        a = get_one_page(url)
        s1 = '"view_price":"(.+?)","view_fee"'
        view_price = re.compile(s1).findall(a)
        #print(view_price)
        print('爬取到' + str(len(view_price)) + '个信息')
        if d1 == 1:
                excel_row2 = 1
        else:
                excel_row2 = (len(view_price) * (d1 - 1) - 8)
        try:
            for j1 in range(0, len(view_price)):
                w.write(excel_row2, 1, view_price[j1])
                w.write(excel_row2, 0, excel_row2)
                excel_row2 += 1
            d1 += 1
        except Exception as e:
                print(e)
        s2 = '(?<="item_loc":").+?(?=","view_sales")'
        place = re.compile(s2).findall(a)
        if d2 == 1:
            excel_row2 = 1
        else:
            excel_row2 = (len(place) * (d2 - 1) - 8)
        try:
            for j2 in range(0, len(place)):
                w.write(excel_row2, 2, place[j2])
                excel_row2 += 1
            d2 += 1
        except Exception as e:
            print(e)
        s3 = '(?<="view_sales":").*?[0-9](?=人付款","comment_count")'
        pay = re.compile(s3).findall(a)
        if d3 == 1:
            excel_row2 = 1
        else:
            excel_row2 = (len(pay) * (d3 - 1) - 8)
        try:
            for j3 in range(0, len(pay)):
                w.write(excel_row2, 3, pay[j3])
                excel_row2 += 1
            d3 += 1
        except Exception as e:
            print(e)
        s4 = '(?<="nick":").+?(?=","shopcard")'
        stop = re.compile(s4).findall(a)
        if d4 == 1:
            excel_row2 = 1
        else:
            excel_row2 = (len(stop) * (d4 - 1) - 8)
        try:
            for j4 in range(0, len(stop)):
                w.write(excel_row2, 4, stop[j4])
                excel_row2 += 1
            d4 += 1
        except Exception as e:
            print(e)
        s5 = '(?<="raw_title":").+?(?=","pic_url")'
        name = re.compile(s5).findall(a)
        if d5 == 1:
            excel_row2 = 1
        else:
            excel_row2 = (len(name) * (d5 - 1) - 8)
        try:
            for j5 in range(0, len(name)):
                w.write(excel_row2, 5, name[j5])
                excel_row2 += 1
            d5 += 1
        except Exception as e:
            print(e)
        ws.save("男裤信息.xls")

def main():
    urls = []
    for i in range(80):
        urls.append(
                'https://s.taobao.com/search?q=%E7%94%B7%E8%A3%A4&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306&bcoffset=4&ntoffset=4&p4ppushleft=1%2C48&s='
                + str(i*44))
    get_information(urls)

if __name__ == '__main__':
    main()
    #pool = Pool()
    #pool.map(main,{i*44 for i in range(2)})
